<?php
class Authentication extends CI_Model
{
	public $tbl_name = 'user';
	public $col_userid = 'userid';
	public $col_username = 'username';
	public $col_password = 'password';

	public function __contruct()
	{
		parent::__construct();	
	}
	

	public function IsAuthenticated_Session(){

		$isLoggedIn = $this->session->userdata('isLoggedIn');
		if ( $isLoggedIn ) {
			return true;
		} 

		else{
			return false;
		}

	}// end IsAuthenticated_Session

	public function IsAuthenticated(){
			
			$username = $this->input->post('username');
			$password = $this->input->post('password');

			$this->db->where( $this->col_username, $username );
			$this->db->where( $this->col_password, $password );
			$this->db->order_by($this->col_userid, "desc");
			$this->db->limit(1);

			$this->db->from($this->tbl_name);

			if( $this->db->count_all_results() > 0){

				/* SESSION */
				$this->session->set_userdata('username', $username);
				$this->session->set_userdata('isLoggedIn', true);
				return true;		
			}// end if

			else{
				return false;
			}
	}// end IsAuthenticated

	public function Logout(){

		$array_items = array('username' => '', 'isLoggedIn' => '');
		$this->session->unset_userdata($array_items);
	}
}
?>